import { ResponseData } from "@/types/common/response";
import { http } from "@/utils/http";
import { OrderDTO, OrderQuery, OrderRequest, OrderVO } from "@/types/attendance/order";

/**
 * 获取订单列表
 * @param query 查询参数
 * @returns 订单列表数据
 */
export function getOrderList(
  query: OrderQuery
): Promise<ResponseData<{ rows: OrderDTO[]; total: number }>> {
  return http.get("/attendance/order/list", { params: query });
}

/**
 * 获取订单详情
 * @param orderId 订单ID
 * @returns 订单详情数据
 */
export function getOrder(orderId: string | number): Promise<ResponseData<OrderDTO>> {
  return http.get(`/attendance/order/${orderId}`);
}

/**
 * 新增订单
 * @param data 订单数据
 * @returns 操作结果
 */
export function addOrder(data: OrderRequest): Promise<ResponseData<void>> {
  return http.post("/attendance/order", { data });
}

/**
 * 修改订单
 * @param data 订单数据
 * @returns 操作结果
 */
export function updateOrder(data: OrderVO): Promise<ResponseData<void>> {
  return http.request("put", "/attendance/order", { data });
}

/**
 * 删除订单
 * @param orderId 订单ID
 * @returns 操作结果
 */
export function deleteOrder(orderId: string | number): Promise<ResponseData<void>> {
  return http.request("delete", `/attendance/order/${orderId}`);
}

/**
 * 导出订单数据
 * @param query 查询参数
 */
export function exportOrder(query: OrderQuery) {
  return http.get("/attendance/order/export", {
    params: query,
    responseType: "blob"
  });
}

/**
 * 更新订单状态
 * @param orderId 订单ID
 * @param status 状态（0进行中 1已完成 2已取消）
 * @returns 操作结果
 */
export function updateOrderStatus(orderId: number, status: string): Promise<ResponseData<void>> {
  return http.request("put", `/attendance/order/status/${orderId}/${status}`);
}

/**
 * 获取订单编号
 */
export function getOrderCode(): Promise<ResponseData<string>> {
  return http.get("/attendance/order/code");
}

/**
 * 获取订单详情（兼容新API）
 * @param orderId 订单ID
 * @returns 订单详情数据
 */
export function getOrderInfo(orderId: number): Promise<ResponseData<OrderDTO>> {
  return getOrder(orderId);
}

/**
 * 获取项目选项列表
 * @returns 项目选项列表
 */
export function getProjectOptions(): Promise<
  ResponseData<Array<{ projectId: number; projectName: string }>>
> {
  return http.get("/attendance/project/options");
}

/**
 * 获取供应商选项列表
 * @returns 供应商选项列表
 */
export function getSupplierOptions(): Promise<
  ResponseData<Array<{ supplierId: number; supplierName: string }>>
> {
  return http.get("/attendance/supplier/options");
}
